#!/bin/bash
#####################################################################
# LNMP is a bash script for the installation of Nginx + PHP + MySQL.#
# Author        Hehl <1181554113@qq.com>                            #
# Blog          Http://www.cnblogs.com/ddif                         #
#####################################################################

echo -e "$GREEN"
echo "                Version:"
echo "+----------------------------------------------------------------------+ "
echo "|      NGINX          |      nginx-1.12.0                                |"
echo " --------------------- ------------------------------------------------- "
echo "|      JAVA           |      ${java_version}                             |"
echo " --------------------- ------------------------------------------------- "
echo "|      MYSQL          |      ${mysql_version[1]} |  ${mysql_version[4]}  |"
echo "+----------------------------------------------------------------------+ "

######################################  Menu Start    ########################################
#Display menu and choose  install or not, then run it
Menu(){
echo  -e "$YELLOW"
while :;do echo
	read -p "Do you want sync Beijing time?(y/n)" sync_time_yn
	if [[ ! $sync_time_yn =~ ^[y,Y,n,N]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		break	
	fi
done
		
while :;do echo
	read -p "Do you want set Aliyun Repo?(y/n)" repo_yn
	if [[ ! $repo_yn =~ ^[y,Y,n,N]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		break
	fi
done

while :;do echo
	read -p "Do you want Install Nginx?(y/n)" install_ngx_yn
	if [[ ! $install_ngx_yn =~ ^[y,Y,n,N]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		break
	fi
done

while :;do echo
	read -p "Do you want Install Mysql?(y/n)" install_mysql_yn
	if [[ ! $install_mysql_yn =~ ^[y,Y,n,N]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		break
	fi
done

if [ "$install_mysql_yn" == "y" -o "$install_mysql_yn" == "Y" ];then
	if [ -d "/usr/local/mysql"  ];then
		echo
		echo -e "${RED}You had installed Mysql Server!$YELLOW"	
	else
		echo -e "Please choose MYSQL version:
		$RED 1)$WHITE ${mysql_version[1]} $GREEN( Default );
		$RED 2)$WHITE ${mysql_version[2]} $GREEN( RAM size must large than 2 GB! );"
		echo -e "$WHITE"
		read -p "Please input the number of your choose:" mysql_version_select
		if [[ -z "$mysql_version_select" || "$mysql_version_select" -eq 1 || "$mysql_version_select" -eq 2 ]];then
			mysql_install_way
			[ -z "$mysql_version_select" ] && mysql_version_select=1
			echo
			read -p "mysql server root password (default:root): " mysql_root_pass
			mysql_root_pass=${mysql_root_pass:=root}
			echo  -e "Mysql root password: ${mysql_root_pass}"
			echo -e "$WHITE"
		else
			echo -e "Input error number for choose mysql version!"
		fi
	fi
fi

while :;do echo
	read -p "Do you want Install Redis?(y/n)" install_redis_yn
	if [[ ! $install_redis_yn =~ ^[y,Y,n,N]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		break
	fi
done

while :;do echo
	read -p "Do you want Install Pureftpd?(Y/n)" install_ftp_yn
	if [[ ! $install_ftp_yn =~ ^[Y,y,N,n]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		break
	fi
done
if [[ "$install_ftp_yn" =~ ^[y,Y]$ ]];then
		echo -e "\033[32m"
		[ -f $ftp_sbin ] && echo -e "\033[32mYou already installed pureftpd in you system!!\033[0m" && exit 0
		while :;do
			read -p "Please input ftp user acount:" ftp_user
			if [ -z "$ftp_user" ];then
				echo
			else
				break
			fi
		done
		echo
		while :;do
			read -p "Please input ftp user $ftp_user password:" ftp_pwd
			if [ -z $ftp_pwd ];then
				echo
			else
				break
			fi
		done
		echo
		read -p "Please input your ftpuser $ftp_user home dirctory: (Default /data/wwwroot)" ftp_user_home
		[ -z $ftp_user_home ] && ftp_user_home="/data/wwwroot"
		echo -e "\033[32m#########################################\033[0m"
fi

while :;do echo
	read -p "Do you want install java?(Y/n)" java_yn
	if [[ ! $java_yn =~ ^[Y,y,n,N]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		break
	fi
done

while :;do echo
	read -p "Do you want install elasticsearch?(Y/n)" es_yn
	if [[ ! $es_yn =~ ^[Y,y,n,N]$ ]];then
		echo -e "\033[0mPlease input y or n\033[33m"
	else
		echo -e "\033[32m"
		[ -d $es_install_dir ] && echo -e "\033[32mYou already installed elasticsearch in you system!!\033[0m" && exit 0
		if [[ $es_yn =~ ^[Y,y]$ ]];then
			java_yn="y"
			while :;do
				read -p "Please input es user password:" es_pwd
				if [ -z $es_pwd ];then
					echo
				else
					break
				fi
			done
			echo
		fi
		break
	fi
done
###################################### Menu End    ########################################
}
Menu

#If you want ask,you can use No_Menu function
No_Menu(){
install_ngx_yn="y"
java_yn="y"
mysql_version_select=1
mysql_root_pass="yunweijilu.com"
install_redis_yn="n"
install_ftp_yn="n"
}
#No_Menu

echo -e "$WHITE"
echo "OS is $os"
echo

#Sync time
sync_time

#Select use Aliyun Repo
case $repo_yn in
y|Y)
        . ./include/set_repo.sh
        ;;
*)
        echo "Not install or input wrong value for Aliyun Repo!"
        ;;
esac

#install nginx
nginx -v 2>/dev/null
if [  $install_ngx_yn == "y" -a $? -eq 0 ];then
	echo "You select install Nginx"
	echo -e "Nginx is already installed,Please check!"
elif	[  "$install_ngx_yn" == "y" ];then
	. ./include/nginx.sh
else
	echo "Not install or input wrong value for Nginx!"
fi

#install mysql
if [[ $mysql_version_select -eq 1 || $mysql_version_select -eq 2 ]];then
        . ./include/mysql.sh
else
	echo "Not install or input wrong value for Mysql!"
fi

#install java
java -version 2>/dev/null
if [  $java_yn == "y" -a $? -eq 0 ];then
	echo "You select install Java"
	echo -e "Java is already installed,Please check!"
elif	[  "$java_yn" == "y" ];then
	yum install java-1.8.0-openjdk-devel.x86_64 -y
else
	echo "Not install or input wrong value for Java!"
fi

#install redis
if [ $install_redis_yn == "y" -o $install_redis_yn == "Y" ];then
	echo "You select install Redis."
	. ./include/redis.sh
else
	echo "Not install or input wrong value for Redis!" 
fi

#install pureftpd
if [ $install_ftp_yn == "y" -o $install_ftp_yn == "Y" ];then
	echo "You select install Pureftpd"
	. ./include/pureftp.sh
fi

#install es
if [  $es_yn == "y" ];then
	. ./include/es.sh
else
	echo "Not install or input wrong value for es!"
fi
